package cn.tedu._06article.base.Interceptor;

import cn.tedu._06article.pojo.vo.UserVO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("开始执行登录检查...");
        HttpSession session = request.getSession();
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO != null){
            log.debug("用户已登录，放行...");
            return true;
        }else{
            log.debug("用户未登录，拦截...");
            request.getRequestDispatcher("/v1/user/login");
            throw new RuntimeException("用户未登录！");
        }
    }
}
